package vm;

/**
 * @author zzb
 * @since 2021-01-21
 */
public class FinalizeEscapeGC {
  public static FinalizeEscapeGC SAVE_HOOK = null;
  public void isAlive() {
    System.out.println("yes,i am still alive");
  }

  @Override
  protected void finalize() throws Throwable {
    super.finalize();
    System.out.println("finalize method executed");
    FinalizeEscapeGC.SAVE_HOOK = this;
  }

  public static void main(String[] args) throws InterruptedException {
    SAVE_HOOK = new FinalizeEscapeGC();
    // 对象第一次成功拯救自己
    SAVE_HOOK = null;
    System.gc();
    // 因为finalize方法优先级很低，所以暂停0.5秒等待他
    Thread.sleep(500);
    if (SAVE_HOOK != null) {
      SAVE_HOOK.isAlive();
    } else {
      System.out.println("no, i am dead");
    }

    // 在自救一次，发现自救失败
    SAVE_HOOK = null;
    System.gc();
    // 因为finalize方法优先级很低，所以暂停0.5秒等待他
    Thread.sleep(500);
    if (SAVE_HOOK != null) {
      SAVE_HOOK.isAlive();
    } else {
      System.out.println("no, i am dead");
    }
  }
  /**
   * finalize method executed
   * yes,i am still alive
   * no, i am dead
   */
}
